package me.zk.practice1.hw;

import java.util.*;

public class HWBJJ18 {
    static List<Integer> res = new ArrayList<>();
    static List<int[]> list = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int[] nums = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();

        int n  = nums[0];
        int m = nums[1];
        if (n < 1 || n > 100) {
            System.exit(1);
        }
        for (int i = 0; i < m; i++) {
            int[] array = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            if (array[0] < 1 || array[1] < 1||array[0]>100||array[1]>100){
                break;
            }
            list.add(array);
        }
        int[] array = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int start  = array[0];
        int end = array[1];
       get(start,end,0);
       if (res.size() == 0) {
           System.out.println(-1);
       } else {
           System.out.println(Collections.min(res));
       }
    }

    public static void get(int s, int e, int count) {

        for (int i = 0; i < list.size(); i++) {
            int[] num = list.get(i);
            if (s == num[0]) {
                count+= num[2];
                if (e == num[1]) {
                    res.add(count);
                } else {
                    get(num[1],e,count);
                }
            }
        }
    }


}
